package com.sojson.project.sys.config.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.project.sys.config.controller.param.verify.ConfigParamVerify;
import com.sojson.project.sys.config.entity.dto.ConfigDto;
import com.sojson.project.sys.config.entity.dto.ConfigPageDto;
import com.sojson.project.sys.config.entity.po.Config;
import com.sojson.project.sys.config.entity.vo.ConfigVo;
import com.sojson.project.sys.config.service.ConfigService;
import com.sojson.result.Result;
import com.sojson.util.SystemParamUtil;
import com.sojson.util.poi.ExcelUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 配置表控制器
 * 
 * @author liu
 * @date 2022-08-26
 */
@Api(tags = "配置表管理")
@RestController
@RequestMapping("/sys/config")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    /**
     * 单条插入
     * 
     * @param dto
     * @return
     * @throws IOException 
     */
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPermi('/sys/config/save')")
    @ApiOperation("给配置表添加一条数据")
    public Result<Boolean> save(@RequestBody Config dto) throws IOException {
        // 校验参数
        Result<Boolean> paramResult = ConfigParamVerify.save(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        dto.saveInit();
        return Result.success(configService.add(dto));
    }

    /**
     * 通过id更新
     * 
     * @param dto
     * @return
     * @throws IOException 
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/sys/config/updateById')")
    @ApiOperation("通过id更新配置表数据")
    public Result<Boolean> updateById(@RequestBody Config dto) throws IOException {
        // 校验参数
        Result<Boolean> paramResult = ConfigParamVerify.updateById(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        dto.updateInit();
        return Result.success(configService.update(dto));
    }

    /**
     * 分页查询
     * 
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermi('/sys/config/page')")
    @ApiOperation(value = "分页查询配置表数据", response = ConfigVo.class)
    public Result<List<ConfigVo>> page(ConfigPageDto dto) {
        return Result.success(configService.page(dto));
    }

    /**
     * 导出数据
     * 
     * @param dto
     * @return
     * @throws Exception 
     */
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermi('/sys/config/export')")
    @ApiOperation("导出配置表数据")
    public void export(ConfigDto dto) throws Exception {
        List<ConfigVo> list = configService.list(dto);
        ExcelUtil<ConfigVo> excelUtil = new ExcelUtil<>(ConfigVo.class);
        excelUtil.exportExcelOutFile(list, "系统配置");
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@ss.hasPermi('/sys/config/info/{id}')")
    @ApiOperation(value = "通过id查询配置表数据", response = ConfigVo.class)
    public Result<ConfigVo> info(@PathVariable String id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(configService.findById(id));
    }

    /**
     * 通过id列表删除
     * 
     * @param dto
     * @return
     * @throws IOException 
     */
    @DeleteMapping("/remove")
    @PreAuthorize("@ss.hasPermi('/sys/config/remove')")
    @ApiOperation("通过id列表删除配置表数据")
    public Result<Boolean> remove(@RequestBody ConfigDto dto) throws IOException {
        List<String> ids = dto.getIds();
        if (ids == null || ids.size() == 0) {
            return Result.success();
        }

        return Result.success(configService.removeByIds(ids));
    }

    /**
     * 通过key查询
     * 
     * @param key
     * @return
     * @throws IOException 
     */
    @GetMapping("/findByKey/{key}")
    @ApiOperation(value = "通过key查询配置表数据", response = ConfigVo.class)
    public Result<ConfigVo> findByKey(@PathVariable String key) throws IOException {
        if (key == null) {
            return Result.success();
        }

        return Result.success(configService.findByKey(key, false));
    }

    /**
     * 清理缓存
     * 
     * @return
     * @throws IOException 
     */
    @DeleteMapping("/clearCache")
    @PreAuthorize("@ss.hasPermi('/sys/config/clear')")
    @ApiOperation("清理配置表缓存")
    public Result<Boolean> clearCache() throws IOException {
        SystemParamUtil.reload();
        return Result.success();
    }

}